package com.ad.pet.app.collect;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.MainActivity;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.collect.adapter.CollectTagAdapter;
import com.ad.pet.app.shopcar.YouLikeContract;
import com.ad.pet.app.shopcar.YouLikePresenter;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;
import com.ad.pet.models.remote.collect.BigArticle;
import com.ad.pet.ui.SimpleDividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liuyixiang on 2019/10/28.
 */

public class ArticleCollectionActivity extends AppToolbarActivity implements YouLikeContract.View{
    @BindView(R.id.recycler_good)
    RecyclerView recyclerGood;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    @Inject
    YouLikePresenter youLikePresenter;
int  page=1;
    private CollectTagAdapter collectTagAdapter;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mStatusLayoutManager.showContent();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_article_collection;
    }

    @Override
    protected void clickRetry(View v) {

    }

    @Override
    protected void initViews() {
        youLikePresenter.onCreate(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        collectTagAdapter = new CollectTagAdapter(null);
        recyclerGood.setLayoutManager(linearLayoutManager);
        recyclerGood.addItemDecoration(new SimpleDividerDecoration(mActivity,3,R.color.bg_color));
        recyclerGood.setAdapter(collectTagAdapter);

        youLikePresenter.getCollectArticleList(String .valueOf(page));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                youLikePresenter.getCollectArticleList(String.valueOf(page));
            }
        });

        collectTagAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                ++page;
                youLikePresenter.getCollectArticleList(String.valueOf(page));
            }
        }, recyclerGood);

        collectTagAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Navigation.goCommonWebView(mActivity,collectTagAdapter.getData().get(position).getTitle(),collectTagAdapter.getData().get(position).getH5_url(),false, MainActivity.REQUEST_GO_MALL);
            }
        });
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void getArticleListSuccess(BigArticle bigArticle) {

    }

    @Override
    public void getCollectArticleListSuccess(BigArticle bigArticle) {
        swipeRefresh.setRefreshing(false);
        if (page == 1) {
            collectTagAdapter.setNewData(bigArticle.getData());
        } else {
            collectTagAdapter.addData(bigArticle.getData());
            collectTagAdapter.loadMoreComplete();
        }
        if (bigArticle.getData().size() < 10) {//CommonConstant.PAGE_COUNT
            //暂无更多数据
            collectTagAdapter.loadMoreEnd();
        }
        if (bigArticle.getData().size() == 0 && page == 1) {
            collectTagAdapter.setEmptyView(R.layout.layout_empty_address);
            TextView textView = collectTagAdapter.getEmptyView().findViewById(R.id.tv_title);
            textView.setText("您还没有相关收藏哦~");
        }
    }

    @Override
    public void collectArticleSuccess() {

    }

    @Override
    public void cancelCollectArticleSuccess() {

    }

    @Override
    public void zanArticletSuccess() {

    }

    @Override
    public void cancelZanArticleSuccess() {

    }

    @Override
    public void articleCommentSuccess() {

    }
}
